package com.example.teaknow.ui.history;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.teaknow.Adapter.itemAdapter;
import com.example.teaknow.Bean.NewsBean;
import com.example.teaknow.Dao.HistoryDao;
import com.example.teaknow.NewsDetailActivity;
import com.example.teaknow.R;

import java.util.ArrayList;

public class HistoryFragment extends Fragment {
    private ArrayList<NewsBean> newsBeans=new ArrayList<NewsBean>();
    private ListView listView;
    private Boolean flag=false;
    private HistoryDao historyDao;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_history, container, false);
        listView=(ListView)root.findViewById(R.id.lv_load_history);
        initView();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getContext(), NewsDetailActivity.class);
                NewsBean newsBean=historyDao.queryHistory().get(position);
                intent.putExtra("newsBean",newsBean);
                startActivity(intent);
            }
        });
        return root;
    }

    private void initView() {
        if(flag){

        }
        else {
            flag=true;
            historyDao=new HistoryDao(getActivity());
            newsBeans=historyDao.queryHistory();
        }
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                listView.setAdapter(new itemAdapter(newsBeans,getContext()));
            }
        });


    }
}